package cn.xiaobai.captcha.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDatePrinter;
import cn.xiaobai.captcha.enums.UnitToCalendarUnitEnum;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author yangdaji
 * @Date 2024/4/19
 * @PackageName cn.xiaobai.captcha.utils
 * @ClassName DateUtils
 */
public class DateUtils {

    public static final String YDMHMD = "yyyy-MM-dd HH:mm:ss";

    public static String dateToStr(Date time) {
        return DateUtil.format(time, YDMHMD);
    }

    public static Date intervalToDate(Date time, long interval, TimeUnit unit) {
        // 将给的时间, 加上当前时间返回
        Calendar instance = Calendar.getInstance();
        instance.setTime(time);
        instance.add(UnitToCalendarUnitEnum.getCalendar(unit), new Long(interval).intValue());
        return instance.getTime();
    }

    public static String intervalToStr(Date time, long interval, TimeUnit unit) {
        // 将给的时间, 加上当前时间返回
        Calendar instance = Calendar.getInstance();
        instance.setTime(time);
        instance.add(UnitToCalendarUnitEnum.getCalendar(unit), new Long(interval).intValue());
        return dateToStr(instance.getTime());
    }
}
